iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 25
0
var connector = new LineConnector({
    hasPushApi: false, //you need to pay push api >.,<
    channelId: config.channelId,
    channelSecret: config.channelSecret,
    channelAccessToken: config.channelAccessToken
});
server.post('/line', connector.listen());

var bot = new builder.UniversalBot(connector)

bot.dialog("/", s => {
    s.beginDialog("start")
})

bot.on('conversationUpdate', function (message) {
    switch (message.text) {
        case 'follow':
            break;
        case 'unfollow':
            break;
        case 'join':
            bot.beginDialog(message.address, "start")
            break;
        case 'leave':
            break;
    }
    console.log("conversationUpdate")

});

bot.dialog("start", s => {
    s.send(new builder.Message(s).addAttachment(new Sticker(s, 1, 2)));
    s.send("遊戲開始")
    s.beginDialog("ask");
})
bot.dialog("ask", [
    (s, args) => {
        let q: {
            "q": string,
            "options": Array<string>,
            "a": string
        };
        if (args && args.q) {
            q = args.q

        } else {
            q = q_list[Math.floor(Math.random() * (q_list.length - 1))];
        }
        s.dialogData.q = q;
        //獲得題目,或是未答對上一題,再打下去。
        builder.Prompts.choice(s, new builder.Message(s)
            .text(q.q)
            .suggestedActions(
            builder.SuggestedActions.create(
                s, q.options.map(o => {
                    return builder.CardAction.imBack(s, o, o)
                })
            )),
            q.options
        );
    },
    (s, r) => {
        s.dialogData.answers = [];
        if (r.response.entity === s.dialogData.q.a) {
            s.send(`${s.message.from.name} 答對了! 加一分!`)
            s.send(new builder.Message(s).addAttachment(new Sticker(s, 1, 2)));
            s.dialogData.q = null
        } else {
            s.send(`${s.message.from.name} 答錯了! 扣一分!`)
            s.send(new builder.Message(s).addAttachment(new Sticker(s, 1, 3)));
        }
        s.replaceDialog("ask", { q: s.dialogData.q })
    }
])

結果這個比較麻煩的部份,是去爬題庫。哈


上一篇
linebot demo : 格式介紹
下一篇
read me 的撰寫
系列文
Microsoft Bot Framework 30天上手 + 自幹 Line Builder 串接 + Line Bot Demo30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言